#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "mtp_pwm.h"
#include "OuadSPI.h"

/********************************************************************************************
********************************************************************************************
    Generate  写寄存器 
    Generate  读寄存器 
********************************************************************************************
********************************************************************************************/
//build Buffer_ReadCode for 


/********************************************************************************************
name :Generate_MIPI_Read 
para0 : pdev
para1 :	SigMode MIPI模式
para2 :	channel port
--------------------------
para3 :	reg 目标回读寄存器
para4 ：backnum 回读个数
para5 ：Rxbuffer 缓存回读数据的数组的指针
********************************************************************************************/
void Generate_MIPI_Read(USBD_HandleTypeDef *pdev,u8 SigMode ,u8 channel,u8 reg ,u8 backnum, u8* Rxbuffer)
{
	Mipi_Read(pdev,SigMode,channel,reg,backnum,Buffer_ReadCode);
	memcpy(Rxbuffer,Buffer_ReadCode,backnum);
}
u8 SPI_flag_bit; //SPI4 读写方式标志位  =1 读写一根线
void Mipi_Read(USBD_HandleTypeDef *pdev,u8 SigMode ,u8 channel,u8 reg ,u8 backnum, u8* Rxbuffer)
{
	u16 tmp=0;
	u8 buffer1[3];
	SSD2828_W_Reg(SigMode,channel,0xC1,backnum); //return package size                                
	SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	buffer1[0] = 0x01;
	buffer1[1] = reg; 	//Download register
	if(SigMode!=QuadSPI_Mode)    
	 SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5); 	
	if(SigMode == Video_Mode)
	{				
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		for(i=0;i<backnum;i++)  //readback num
		{
			SSD2828_W_Cmd(SigMode,channel,0xFA);
			tmp=SPI3_Read_u16_Data(channel);
			Rxbuffer[i]=tmp>>8;
			Rxbuffer[1+i]=tmp;
			delay_ms(5);
			i++;
		}
	}
	else if(SigMode == CMD_Mode)
	{		
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		for(i=0;i<backnum;i++)  //readback num
		{
			if(i==0)
				tmp=SSD2828_R_Reg(SigMode,channel,0xFF);
			else
				tmp=SSD2828_R_Reg_Flow(SigMode,channel);				
			Rxbuffer[i]=tmp;
			Rxbuffer[1+i]=tmp>>8;
			delay_ms(10);	
			i++;
		}				
	}
	else if(SigMode == SPI4_Mode)
	{
	  if(SPI_flag_bit==0x01) //SPI4 读写一根线
		{
				tmp=GenR_SPI4_Read_u8_Data_3line(SigMode,channel,reg,Rxbuffer,backnum);//readback num	
		}
	  else if(SPI_flag_bit==0x02) //SPI4 读写一根线 （限page 00）
		{
				tmp=GenR_SPI4_Read_u8_Data_3line_Page0(SigMode,channel,reg,Rxbuffer,backnum);//readback num	
		}
		else if(SPI_flag_bit == 0x03)
		{
		    tmp=SPI4_PANEL_Read_u8_Data_array_GenWR(SigMode,channel,reg,Rxbuffer,backnum);//readback num
		}
		else
		{
				tmp=GenR_PANEL_SPI4_Read_u8_Data(SigMode,channel,reg,Rxbuffer,backnum);//readback num
		}

	}	
	
	else if(SigMode==QuadSPI_Mode||SigMode==QuadSPI_DDR_Mode)//DDR_READ
	{
	  OSPI_Read_Array_Data(SigMode,channel,reg,Rxbuffer,backnum);
	}
}

void Mipi_Read_U16(USBD_HandleTypeDef *pdev,u8 SigMode ,u8 channel,u8 regpage,u8 regaddr ,u8 backnum, u8* Rxbuffer)
{
	u8 tmpxx=0;
	u8 buffer1[4];	
	SSD2828_W_Reg(SigMode,channel,0xC1,backnum); //return package size                                
//	SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	buffer1[0] = 0x02;
	buffer1[1] = regpage;//regpage; 	//Download register   page
	buffer1[2] = regaddr;//regaddr; 	//Download register   address
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5); 	
	if(SigMode == Video_Mode)
	{				
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		for(i=0;i<backnum;i++)  //readback num
		{
			SSD2828_W_Cmd(SigMode,channel,0xFA);
			tmpxx = SPI3_Read_u8_Data(channel);
			Rxbuffer[i]=tmpxx;
			Rxbuffer[1+i]=tmpxx>>8;
			delay_ms(5);
			i++;
		}
	}
	else if(SigMode == CMD_Mode)
	{		
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		for(i=0;i<backnum;i++)  //readback num
		{
			if(i==0)
				tmpxx=SSD2828_R_Reg(SigMode,channel,0xFF);
			else
				tmpxx=SSD2828_R_Reg_Flow(SigMode,channel);				
			Rxbuffer[i]=tmpxx;
			Rxbuffer[1+i]=tmpxx>>8;
			delay_ms(10);	
			i++;
		}				
	}
	else if(SigMode == SPI4_Mode)
	{
		tmpxx=GenR_PANEL_SPI4_Read_u8_Data(SigMode,channel,regpage,Rxbuffer,backnum);//readback num
	}	
}
u16  MipiCommandModeSelect=0x0740;	//LP_DCS_Long_Write;	
void Generate_Write_Function(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if((buffer[1]&0x80)!=0x80)
		{
			if((buffer[1]&0x0f)==0x08)//LP_Gerneric_Short_Write
			{MipiCommandModeSelect=LP_Gerneric_Short_Write; SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x09)//HS_Gerneric_Short_Write
			{MipiCommandModeSelect=HS_Gerneric_Short_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x0A)//LP_DCS_Short_Write
			{MipiCommandModeSelect=LP_DCS_Short_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x0D)//HS_DCS_Short_Write	
			{MipiCommandModeSelect=HS_DCS_Short_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}	
			else if((buffer[1]&0x0f)==0x0C)//LP_Gerneric_Long_Write
			{MipiCommandModeSelect=LP_Gerneric_Long_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x0D)//HS_Gerneric_Long_Write
			{MipiCommandModeSelect=HS_Gerneric_Long_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x0E)//LP_DCS_Long_Write
			{MipiCommandModeSelect=LP_DCS_Long_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}
			else if((buffer[1]&0x0f)==0x0F)//HS_DCS_Long_Write	
			{MipiCommandModeSelect=HS_DCS_Long_Write;SSD2828_W_Reg(SigMode,channel,0xB7,MipiCommandModeSelect);}	
//			delay_ms(5);			
	//------------------------------------------------------																	
			SSD2828_W_Array(SigMode,channel,buffer,2);
            buffer[1]=0x02;
//            buffer[0]=0x99;
			STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);//返回 读取的 寄存器数据  ：ok 
//			delay_ms(5);
		}
		else if((buffer[1]&0x80)==0x80)//结束标志位
		{
			buffer[0]=0x99;
			buffer[1]=0x01;
			buffer[4] = Uart_Error_None;
			STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 	
			if(SigMode == Video_Mode)			//||(SigMode == CMD_Mode)
			{				
				SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);		
			}
		}
}

void Generate_Read_Function(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 read_back=buffer[4];
		u8 readbit=buffer[1]&0x40;   //8bit = 0x00 ; 16bit =0x40 的区分
		if((buffer[1]&0x80)!=0x80)
		{			
			if((buffer[1]&0x0f)==0x00)//LP_Gerneric_Short_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,LP_Gerneric_Short_Read);}
			else if((buffer[1]&0x0f)==0x01)//HS_Gerneric_Short_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,HS_Gerneric_Short_Read);}
			else if((buffer[1]&0x0f)==0x02)//LP_DCS_Short_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Read);}
			else if((buffer[1]&0x0f)==0x03)//HS_DCS_Short_Read	
			{SSD2828_W_Reg(SigMode,channel,0xB7,HS_DCS_Short_Read);}	
			else if((buffer[1]&0x0f)==0x04)//LP_Gerneric_Long_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,LP_Gerneric_Long_Read);}
			else if((buffer[1]&0x0f)==0x05)//HS_Gerneric_Long_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,HS_Gerneric_Long_Read);}
			else if((buffer[1]&0x0f)==0x06)//LP_DCS_Long_Read
			{SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);}
			else if((buffer[1]&0x0f)==0x07)//HS_DCS_Long_Read	
			{SSD2828_W_Reg(SigMode,channel,0xB7,HS_DCS_Long_Read);}		
			if((SigMode != CMD_Mode)&&(SigMode != Video_Mode))
			{
				SPI_flag_bit=buffer[1]&0x0f;
			}			
			delay_ms(5);
	//------------------------------------------------------	
			if(readbit == 0x00)   //8bit read
			{
//				buffer[3] = SSD2828_W_Array_SPI(SigMode, channel, buffer, 5);
//                Mipi_Read(pdev,SigMode,channel,buffer[3],read_back,Buffer_ReadCode);
//                memcpy(buffer+4,Buffer_ReadCode,read_back);
//                buffer[1]=0x02;
                SSD2828_W_Array(SigMode, channel, buffer, 2);
				buffer[1]=0x02;
			}
			else if(readbit == 0x40)   //16bit  read
			{
				SSD2828_W_Reg(SigMode,channel,0xB7,0x0200);
				delay_ms(15);
				SSD2828_W_Reg(SigMode,channel,0xB7,0x0280);
				delay_ms(3);			
				Mipi_Read_U16(pdev,SigMode,channel,buffer[3],buffer[4],1,Buffer_ReadCode);
				buffer[5] = Buffer_ReadCode[0];		      //16bit读出来的值
				buffer[1]=0x03;    //区分16bit
			}
            else
            {
                SSD2828_W_Array(SigMode, channel, buffer, 2);
				buffer[1]=0x02;
            }
			buffer[0]=0x99;
			STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);//返回 读取的 寄存器数据  ：ok 
		}
		delay_ms(5);
		if(SigMode == Video_Mode)		//||(SigMode == CMD_Mode)
		{		
			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		}
}

void Generate_Pmu_Process(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 pmu_operate = buffer[3];
		double vpp_vol =0.0;
		if(pmu_operate ==0x00) //VPP OFF
		{
			//OTP_POWER_Init(0.0,OTP_Disable);	
			VPP_POWER_Init(0.0,OTP_Disable);	
		}	
		else if(pmu_operate ==0x01) //VPP ON
		{			
			vpp_vol=(double)(((buffer[4]<<8)+buffer[5])*0.01);
			//OTP_POWER_Init(vpp_vol,OTP_Enable);
			VPP_POWER_Init(vpp_vol,OTP_Enable);	
		}
		buffer[0]=0x99;
		buffer[1]=0x04;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);//返回 读取的 寄存器数据  ：ok 
}

void ProcessForIc99( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x30)
	{
			case 0x10:                                      //写寄存器
				Generate_Write_Function(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x20:                                      //读寄存器
				Generate_Read_Function(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x30:	
				Generate_Pmu_Process(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;					
			default:	break;	
	}			
}


